<?php


namespace App\Http\Controllers\Client;


use App\Http\Controllers\Controller;
use App\Models\User;
use Carbon\Carbon;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Session;
use Tymon\JWTAuth\Facades\JWTAuth;

class Login extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login','refresh']]);
    }

    /**
     * use   登陆页
     * user  Hugh
     * date  2021/11/19 11:59
     */
    public function loginView()
    {

        return view('welcome');
    }

    /**
     * use   登陆
     * user  Hugh
     * date  2021/11/19 10:28
     * @param Request $request
     * @return array
     */
    public function Login(Request $request)
    {

        $request->validate([
            'client'=>'required',
            'captcha'=>'required',
            'account'=>'required|min:4|max:8',
            'password'=>'required|min:4|max:8'
        ]);
        $params = $request->all();
        // 验证码
        $captcha = Cache::get($params['client']);
        if(strtolower($captcha) != strtolower($params['captcha'])){
            return ['code'=>1,'message'=>'验证码错误'];
        }else{
            Cache::forget($params['client']);
        }
        //jwt
        $userInfo = User::query()->where(['account'=>$params['account']])->first();
        if(!$userInfo || Hash::check($params['password'],$userInfo['password'].' ')) return ['code'=>1,'message'=>'账号密码错误'];

        if (empty($userInfo) || !$token = JWTAuth::fromUser($userInfo)) {
            return ['code'=>1,'无权限'];
        }

//        //单点登录
        $request->session()->put('userId',$userInfo['id']);
        Cache::put($userInfo['id'],$token);

        return ['code'=>0,'userInfo'=>$userInfo,'token'=>$token];

    }

    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth('client')->factory()->getTTL() * 60
        ]);
    }

    /**
     * use   退出登录
     * user  Hugh
     * date  2021/11/19 10:28
     */
    public function loginOut()
    {
        $data = auth('client')->logout();
        return ['code'=>0,'退出成功'];
    }

    /**
     * use   我的详细信息
     * user  Hugh
     * date  2021/11/19 10:29
     */
    public function myInfo()
    {
        return response()->json(auth('client')->user());
    }

    /**
     * use   获取验证码
     * user  Hugh
     * date  2021/11/18 15:26
     */
    public function getCaptcha()
    {
        $phrase = new PhraseBuilder();
        $code = $phrase->build(4);
        $builder = new CaptchaBuilder();
        $builder->setBackgroundColor(255,200,200);
        $builder->setMaxAngle(0);
        $builder->setMaxBehindLines(10);
        $builder->setMaxFrontLines(10);
        $builder->setTextColor(50, 0, 20);
        $builder->build($width = 150, $height = 40, $font = null);
        $phrase = $builder->getPhrase();
        $client_id = md5(rand(1,1000).time());
        Cache::put($client_id,$phrase,Carbon::now()->addMinutes(10));
        $data = [
            'client'=>$client_id,
            'captcha'=>$builder->inline(),
        ];
        return $data;

    }

    public function getUser()
    {
        $user = User::query()->first();
    }


}

